﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iCollect.Constants;
using iCollect.Database.RepositoryDataSetTableAdapters;
using iCollect.CustomComponent;
using iCollect.util;

namespace iCollect.CollectDetail
{
    public partial class DeviceInfoConfig : UserControl
    {

        private DataTable dt1;
        private DataTable dt2;
        private DataTable dt3;
        private DataTable dt4;
        private DataTable dt5;
        private BaseDeviceCeDian ds = null;

        public DeviceInfoConfig()
        {
            InitializeComponent();
        }

        private void btClickHandler(object sender, System.EventArgs a)
        {
            //textBox7.Text = "测点1";
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        public void loadDevice()
        {
            //this.deviceTableAdapter.Fill(this.cUPDataDataSet.device);
            loadSample();
            loadTabs();
            loadOverview();
            loadDetail();
        }

        private void loadSample()
        {
            ds = UIUtil.getInstance().loadSample(ds, panel2, btClickHandler);
            //ds.Click += btClickHandler;
        }

        private void loadTabs()
        {
            if (GlobalConstants.type == "0")
            {
                if (this.tabControl1.TabPages["tabPage2"] == null)
                {
                    this.tabControl1.TabPages.Add(tabPage2);
                }
                if (this.tabControl1.TabPages["tabPage3"] == null)
                {
                    this.tabControl1.TabPages.Add(tabPage3);
                }

                if (this.tabControl1.TabPages["tabPage4"] != null)
                {
                    this.tabControl1.TabPages.Remove(tabPage4);
                }
                if (this.tabControl1.TabPages["tabPage5"] != null)
                {
                    this.tabControl1.TabPages.Remove(tabPage5);
                }
                if (this.tabControl1.TabPages["tabPage6"] != null)
                {
                    this.tabControl1.TabPages.Remove(tabPage6);
                }
            }
            else
            {
                if (this.tabControl1.TabPages["tabPage2"] != null)
                {
                    this.tabControl1.TabPages.Remove(tabPage2);
                }
                if (this.tabControl1.TabPages["tabPage3"] != null)
                {
                    this.tabControl1.TabPages.Remove(tabPage3);
                }

                if (this.tabControl1.TabPages["tabPage4"] == null)
                {
                    this.tabControl1.TabPages.Add(tabPage4);
                }
                if (this.tabControl1.TabPages["tabPage5"] == null)
                {
                    this.tabControl1.TabPages.Add(tabPage5);
                }
                if (this.tabControl1.TabPages["tabPage6"] == null)
                {
                    this.tabControl1.TabPages.Add(tabPage6);
                }
            }
        }


        private void loadOverview()
        {
            code.Text = GlobalConstants.assetCode;
            textBox6.Text = GlobalConstants.typeLabel;
            textBox1.Text = GlobalConstants.owner;
            dateTimePicker1.Value = GlobalConstants.sDate;
        }

        private void loadDetail()
        {

            string deviceCode = GlobalConstants.currentDevice;

            if (GlobalConstants.type == "0")
            {
                dt1 = devicedetail1TableAdapter.GetDataBy(deviceCode);
                dt2 = devicedetail2TableAdapter.GetDataBy(deviceCode); 

                if (dt1.Rows.Count == 0)
                {
                    insertDetail1();
                }
                else
                {
                    loadDetail1();
                }
                if (dt2.Rows.Count == 0)
                {
                    insertDetail2();
                }
                else
                {
                    loadDetail2();
                }
            }
            else
            {
                dt3 = devicedetail3TableAdapter.GetDataBy(deviceCode);
                dt4 = devicedetail4TableAdapter.GetDataBy(deviceCode);
                dt5 = devicedetail5TableAdapter.GetDataBy(deviceCode);

                if (dt3.Rows.Count == 0)
                {
                    insertDetail3();
                }
                else
                {
                    loadDetail3();
                }
                if (dt4.Rows.Count == 0)
                {
                    insertDetail4();
                }
                else
                {
                    loadDetail4();
                }
                if (dt5.Rows.Count == 0)
                {
                    insertDetail5();
                }
                else
                {
                    loadDetail5();
                }
            }

        }

        private void insertDetail1()
        {
            try
            {
                devicedetail1TableAdapter.Insert("", GlobalConstants.currentDevice, "", "", "");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            
        }

        private void insertDetail2()
        {
            try
            {
                devicedetail2TableAdapter.Insert("", GlobalConstants.currentDevice, "", "", "");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            
        }

        private void insertDetail3()
        {
            try
            {
                devicedetail3TableAdapter.Insert("", GlobalConstants.currentDevice, "", "", "");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

        private void insertDetail4()
        {
            try
            {
                devicedetail4TableAdapter.Insert("", GlobalConstants.currentDevice, "", "", "");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

        private void insertDetail5()
        {
            try
            {
                devicedetail5TableAdapter.Insert("", GlobalConstants.currentDevice, "", "", "");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

        private void loadDetail1()
        {
            try
            {
                DataRow row = dt1.Rows[0];
                textBox5.Text = row[0].ToString();
                textBox3.Text = row[2].ToString();
                textBox7.Text = row[3].ToString();
                textBox8.Text = row[4].ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

        private void loadDetail2()
        {
            try
            {
                DataRow row = dt2.Rows[0];
                textBox10.Text = row[0].ToString();
                textBox11.Text = row[2].ToString();
                textBox9.Text = row[3].ToString();
                textBox4.Text = row[4].ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }


        private void loadDetail3()
        {
            try
            {
                DataRow row = dt3.Rows[0];
                textBox14.Text = row[0].ToString();
                textBox15.Text = row[2].ToString();
                textBox13.Text = row[3].ToString();
                textBox12.Text = row[4].ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

        private void loadDetail4()
        {
            try
            {
                DataRow row = dt4.Rows[0];
                textBox18.Text = row[0].ToString();
                textBox19.Text = row[2].ToString();
                textBox17.Text = row[3].ToString();
                textBox16.Text = row[4].ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

        private void loadDetail5()
        {
            try
            {
                DataRow row = dt5.Rows[0];
                textBox22.Text = row[0].ToString();
                textBox23.Text = row[2].ToString();
                textBox21.Text = row[3].ToString();
                textBox20.Text = row[4].ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

        private void button2_Click(object sender, EventArgs e)  //保存设备配置信息
        {
            try
            {
                deviceTableAdapter.UpdateQuery(code.Text, dateTimePicker1.Value, textBox1.Text, GlobalConstants.currentDevice);

                GlobalConstants.assetCode = code.Text;
                GlobalConstants.owner = textBox1.Text;
                GlobalConstants.sDate = dateTimePicker1.Value;

                UIUtil.getInstance().alert("保存成功。");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                devicedetail1TableAdapter.UpdateQuery(this.textBox5.Text, this.textBox3.Text,
                    this.textBox7.Text, this.textBox8.Text, GlobalConstants.currentDevice);
                dt1 = devicedetail1TableAdapter.GetDataBy(GlobalConstants.currentDevice);

                UIUtil.getInstance().alert("保存成功。");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                devicedetail2TableAdapter.UpdateQuery(this.textBox10.Text, this.textBox11.Text,
                    this.textBox9.Text, this.textBox4.Text, GlobalConstants.currentDevice);
                dt2 = devicedetail2TableAdapter.GetDataBy(GlobalConstants.currentDevice);

                UIUtil.getInstance().alert("保存成功。");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            loadOverview();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            loadDetail1();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            loadDetail2();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                devicedetail3TableAdapter.UpdateQuery(this.textBox14.Text, this.textBox15.Text,
                    this.textBox13.Text, this.textBox12.Text, GlobalConstants.currentDevice);
                dt3 = devicedetail3TableAdapter.GetDataBy(GlobalConstants.currentDevice);

                UIUtil.getInstance().alert("保存成功。");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            try
            {
                devicedetail4TableAdapter.UpdateQuery(this.textBox18.Text, this.textBox19.Text,
                    this.textBox17.Text, this.textBox16.Text, GlobalConstants.currentDevice);
                dt4 = devicedetail4TableAdapter.GetDataBy(GlobalConstants.currentDevice);

                UIUtil.getInstance().alert("保存成功。");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            try
            {
                devicedetail5TableAdapter.UpdateQuery(this.textBox22.Text, this.textBox23.Text,
                    this.textBox21.Text, this.textBox20.Text, GlobalConstants.currentDevice);
                dt5 = devicedetail5TableAdapter.GetDataBy(GlobalConstants.currentDevice);

                UIUtil.getInstance().alert("保存成功。");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            loadDetail3();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            loadDetail4();
        }

        private void button11_Click(object sender, EventArgs e)
        {
            loadDetail5();
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }
        
    }
}
